/**
 * 
 */
package com.youth.fund.hk.hostManager.manager.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.youth.fund.hk.hostManager.manager.datamodel.FlashModel;
import com.youth.fund.hk.hostManager.manager.service.FlashService;
import com.youth.fund.hk.hostManager.manager.service.UploadService;

/**
 * @author Sunk_Sky
 *
 *         2014年12月18日下午8:26:59
 */
@Controller
@RequestMapping(value = "/flash")
public class FlashController
{
	@Autowired
	@Qualifier("FlashService")
	private FlashService flashService;

	@Autowired
	@Qualifier("UploadService")
	private UploadService uploadService;

	@RequiresPermissions("flash:view")
	@RequestMapping(method = RequestMethod.GET)
	public String showList(Model model)
	{
		FlashModel condition = new FlashModel();

		condition.setAvailable(true);

		List<FlashModel> flashList = this.flashService.query(condition);

		model.addAttribute("flash_list", flashList);

		return "flash/list";
	}

	@RequiresPermissions("flash:delete")
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
	public String deleteFlash(@PathVariable int id, Model model,
			RedirectAttributes redirectAttributes)
	{
		FlashModel condition = new FlashModel();

		condition.setId(id);

		boolean result = this.flashService.delete(condition);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "刪除成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "刪除失敗");
		}

		return "redirect:/flash";
	}

	@RequiresPermissions("flash:fresh")
	@RequestMapping(value = "/fresh", method = RequestMethod.GET)
	public String showFreshView(Model model)
	{
		FlashModel condition = new FlashModel();

		condition.setAvailable(true);

		FlashModel flashModel = this.flashService.queryOne(condition);

		if (flashModel == null || flashModel.getId() == 0)
		{
			flashModel = new FlashModel();
		}

		model.addAttribute("flash", flashModel);

		model.addAttribute("op", "确定");

		return "flash/fresh";
	}

	@RequiresPermissions("flash:fresh")
	@RequestMapping(value = "/fresh", method = RequestMethod.POST)
	public String fresh(FlashModel model,
			RedirectAttributes redirectAttributes,
			@RequestParam("file") MultipartFile file)
	{
		String url = "";

		if (file.isEmpty())
		{
			redirectAttributes.addFlashAttribute("msg", "新增失敗,上传文件为空");

			return "redirect:/flash";
		}

		url = this.uploadService.uploadFlash(file, model.getName());
		
		model.setUrl(url);
		
		boolean result = this.flashService.freshFlash(model);

		if (result)
		{
			redirectAttributes.addFlashAttribute("msg", "新增成功");
		} else
		{
			redirectAttributes.addFlashAttribute("msg", "新增失敗");
		}

		return "redirect:/flash";
	}
}
